package com.appcenter.application.controller;


import com.appcenter.application.input.dic.*;
import com.appcenter.application.service.DicService;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/jzappcenter/dic")
@Api("字典操作API")
public class DicController {
    @Autowired
    DicService dicService;

    @ApiOperation(value = "创建字典", notes = "创建字典",produces = "application/json")
    @PostMapping("/createdic")
    public ResponseEntity<JsonResult> createDic(@RequestBody CreateDicInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dicService.CreateDic(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除字典", notes = "删除字典",produces = "application/json")
    @PostMapping("/deletedic")
    public ResponseEntity<JsonResult> deleteDic(@RequestBody DeleteDicInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dicService.DeleteDic(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改字典", notes = "修改字典",produces = "application/json")
    @PostMapping("/editdic")
    public ResponseEntity<JsonResult> editDic(@RequestBody EditDicInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dicService.EditDic(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "搜索字典", notes = "搜索字典",produces = "application/json")
    @PostMapping("/getdics")
    public ResponseEntity<JsonResult> getDics(@RequestBody GetDicsInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(dicService.GetDics(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载字典信息", notes = "加载字典信息",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(dicService.PageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

}
